package com.genersoft.iot.vmp.task;

import com.alibaba.fastjson.JSONObject;
import com.genersoft.iot.vmp.gb28181.transmit.request.impl.MessageRequestProcessor;
import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Component
@Service
public class TimingServiceImpl implements TimingService {

    private final static Logger logger = LoggerFactory.getLogger(MessageRequestProcessor.class);

    @Autowired
    private IRedisCatchStorage redisCatchStorage;

    private int times[] = {5, 10, 20};

    @Autowired
    private RestTemplate mRestTemplate;

    @Scheduled(cron = "*/1 * * * * ?")
    @Override
    public void timingTask() {
        dealalarm();
    }

    @Scheduled(cron = "*/1 * * * * ?")
    @Override
    public void timeTask() {
    }

    /**
     * 报警处理
     */
    private void dealalarm() {
        Integer queuesize = DetectMessageList.getMessageNumber();
        try {
            while (queuesize > 0) {
                JSONObject json = null;
                json = DetectMessageList.consume();
                queuesize = DetectMessageList.getMessageNumber();
                int puttime = json.getIntValue("puttime");
                if (puttime < 3) {
                    String method = json.getString("method");
                    if ("ops".equals(method)) {
                        sendJsonPOST("http://127.0.0.1:8125/api", json);
                    } else if ("inspection".equals(method)) {
                        sendJsonPOST("http://127.0.0.1:8125/state", json);
                    }
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            logger.info("TimingServiceImpl dealalarm error:" + e.toString());
        }
    }

    /**
     * 发送http请求
     *
     * @param json
     * @return
     */
    public String sendJsonPOST(String url, JSONObject json) {
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<String> formEntity = null;
        if (json != null) {
            formEntity = new HttpEntity<>(json.toString(), headers);
        }
        String result = null;
        try {
            result = mRestTemplate.postForEntity(url, formEntity, String.class).getBody();
        } catch (Exception e) {
            logger.info(String.format("execute local detect post api url %s error %s", url, e.toString()));
        }

        return result;
    }
}

